<?php

namespace application\admin\controller;

use ticky\controller;
use ticky\helper\dir;

class tools extends controller {

    /**
     * 重置admin密码
     */
    public function admin() {
        $password = password(get_config('default_password'));
        $where = '`password`="' . $password . '"';
        $sql = "update tky_member set " . $where . " where username='admin' ";
        echo $sql . '<br>';
        db('')->query($sql);
        echo '更新成功';
    }

    /**
     * 清除缓存
     */
    public function clear_cache() {
        $step = isset($_GET['step']) ? intval($_GET['step']) : 1;
        if ($step == 1) {
            cache('categoryinfo', null);
            cache('mapping', null);
            cache('menu_string_' . session('roleid'), null);
            cache('setting', null);
            cache('site', null);
            cache('member_group', null);
            cache('urlrule', null);
            cache('alipay', null);
            cache('1_model_string', null);
            cache('2_model_string', null);
            cache('3_model_string', null);

            showmsg('正在进行数据缓存清空,请稍后...', '/admin/tools/clear_cache/?step=2');
        } elseif ($step == 2) {
            dir::deleteSubFile(RUNTIME_PATH . DS . 'cache');
            dir::deleteSubFile(RUNTIME_PATH . DS . 'compile');
            dir::deleteSubFile(RUNTIME_PATH . DS . 'log');
            dir::deleteSubFile(RUNTIME_PATH . DS . 'temp');
            showmsg('正在进行文件数据缓存清空,请稍后...', '/admin/tools/clear_cache/?step=3');
        } elseif ($step == 3) {
            showmsg('清除缓存成功', '/admin/index/public_home');
        }
    }

    /**
     * 文件校验
     */
    public function filecheck() {
        global $_G;
        $operation = \ticky\request::get('operation', 'filecheck');
        $this->assign('operation', $operation);
        if ($operation == 'filecheck') {
            if (\ticky\request::isPost()) {
                if (!$tickyfiles = @file(RUNTIME_PATH . './tickyphp.md5')) {
                    showmsg('filecheck_nofound_md5file', '/admin/tools/filecheck/?operation=filecheck');
                }
                $_G["md5data"] = null;
                checkfiles('');
                $md5data = $_G["md5data"];
                foreach ($tickyfiles as $line) {
                    $file = trim(substr($line, 34));
                    $md5datanew[$file] = substr($line, 0, 32);
                    if ($md5datanew[$file] != $md5data[$file]) {
                        $modifylist[$file] = $md5data[$file];
                    }
                    $md5datanew[$file] = $md5data[$file];
                }

                $weekbefore = TIMESTAMP - 604800;
                $addlist = @array_merge(@array_diff_assoc($md5data, $md5datanew), $cachelist[2]);
                $dellist = @array_diff_assoc($md5datanew, $md5data);
                $modifylist = @array_merge(@array_diff_assoc($modifylist, $dellist), $cachelist[1]);
                $showlist = @array_merge($md5data, $md5datanew, $cachelist[0]);
                $doubt = 0;
                $dirlist = $dirlog = array();
                foreach ($showlist as $file => $md5) {
                    $dir = dirname($file);
                    if (@array_key_exists($file, $modifylist)) {
                        $fileststus = 'modify';
                    } elseif (@array_key_exists($file, $dellist)) {
                        $fileststus = 'del';
                    } elseif (@array_key_exists($file, $addlist)) {
                        $fileststus = 'add';
                    } else {
                        $filemtime = @filemtime($file);
                        if ($filemtime > $weekbefore) {
                            $fileststus = 'doubt';
                            $doubt++;
                        } else {
                            $fileststus = '';
                        }
                    }
                    if (file_exists($file)) {
                        $filemtime = @filemtime($file);
                        $fileststus && $dirlist[$fileststus][$dir][basename($file)] = array(number_format(filesize($file)) . ' Bytes', $filemtime);
                    } else {
                        $fileststus && $dirlist[$fileststus][$dir][basename($file)] = array('', '');
                    }
                }
                debug($dirlist);
            }
            $this->assign('item', $dirlist);
            $this->display('tools/filecheck');
        } elseif ($operation == 'createfilemd5') {
            //生成md5校验文件
            if (\ticky\request::isPost()) {
                checkfiles('');
                savecheckfiles();
                showmsg('生成md5校验文件成功', '/admin/tools/filecheck/?operation=createfilemd5');
            } else {
                $this->display('tools/createfilemd5');
            }
        }
    }

    /**
     * 文件权限效验
     */
    public function fileperms() {
        $step = max(1, intval($_GET['step']));
        $this->assign('step', $step);
        if ($step == 2) {
            showmsg('正在进行文件效验,请稍后...', '/admin/tools/fileperms/?step=3');
        } elseif ($step == 3) {
            $entryarray = array(
                'avatar',
                'cache',
                'compile',
                'databack',
                'log',
                'uploads'
            );
            $result = '';
            foreach ($entryarray as $entry) {
                $fullentry = RUNTIME_PATH . $entry;
                if (!is_dir($fullentry) && !file_exists($fullentry)) {
                    continue;
                } else {
                    if (!dir_writeable($fullentry)) {
                        $result .= '<li class="error">' . (is_dir($fullentry) ? L('dir') : L('file')) . " ./$entry " . L('fileperms_unwritable') . '</li>';
                    }
                }
            }
            $result = $result ? $result : '<li>' . L('fileperms_check_ok') . '</li>';
            $this->assign('result', '<div class="colorbox"><ul class="fileperms">' . $result . '</ul></div>');
        }
        $this->display('tools/fileperms');
    }

}
